package bstsetcode;


import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/17 16:28
 * E必须要具有可比较性，所以要添加约束
 */
public class BST<E extends Comparable<E>> {

    private class Node{
        public E e;
        private Node left;
        private Node right;

        public Node(E e) {
            this.e = e;
            this.left = null;
            this.right = null;
        }
    }


    private Node root;
    private int size;
    public BST(){
        root = null;
        size = 0;
    }

    private int size(){
        return size;
    }


    public boolean isEmpty(){
        return size ==0;
    }
//    //向二分搜索树种添加新的元素
//    public void add(E e){
//        if(root == null){
//            root = new Node(e);
//            size++;
//        }else{
//            add(root,e);
//        }
//    }
    //向以node为根的SBT中插入元素e，递归算法
//    private void add(Node node, E e) {
//        if(e.equals(node.e)){
//            return;
//        }else if(e.compareTo(node.e) < 0 && node.left == null){
//            node.left = new Node(e);
//            size++;
//            return;
//        }else if(e.compareTo(node.e) > 0 && node.right == null){
//            node.right = new Node(e);
//            size++;
//            return;
//        }
//
//        if(e.compareTo(node.e) < 0){
//            add(node.left,e);
//        }else{
//            add(node.right,e);
//        }
//
//    }


    public void add(E e){
        root = add(root,e);
    }

    //向以node为根的SBT中插入元素e，递归算法
    private Node add(Node node, E e) {
       if (node == null){
           size++;
           return new Node(e);
       }

        if(e.compareTo(node.e) < 0){
           node.left= add(node.left,e);
        }else if(e.compareTo(node.e) > 0){
            node.right= add(node.right,e);
        }
        return node;
    }

    //看BST中是否包含e
    public boolean contains(E e){
        return contains(root,e);
    }

    private boolean contains(Node node, E e) {
        if(node ==null){
            return  false;
        }

        if(e.compareTo(node.e) == 0){
            return true;
        }else if(e.compareTo(node.e) < 0){
            return contains(node.left,e);
        }else{
            return contains(node.right,e);
        }
    }


    //二分搜索树的前序遍历
    public void preOrder(){
        preOrder(root);
    }

    //前序遍历以node为根的二分搜索树--递归算法
    private void preOrder(Node node) {
        if(node ==null){
            return;
        }

        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }




    //二分搜索树的前序遍历
    public void inOrder(){
        inOrder(root);
    }

    private void inOrder(Node node) {
        if(node == null){
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }


    //二分搜索树--非递归的前序遍历
    public void preOrder2(){
        Deque<Node> stack = new ArrayDeque<>();
        stack.push(root);
        while (!stack.isEmpty()){
            Node cur = stack.pop();
            System.out.println(cur.e);
            if(cur.right !=null){
                stack.push(cur.right);
            }
            if(cur.left != null){
                stack.push(cur.left);
            }

        }
    }

    //层序遍历
    public void levelOrder(){
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            Node cur = queue.remove();
            System.out.println(cur.e);
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right !=null){
                queue.offer(cur.right);
            }

        }
    }







    //后序遍历
    public  void postOrder(){
        postOrder(root);
    }

    private void postOrder(Node node){
        if(node == null){
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }





    //寻找BST的最小元素
    public E  minNode(){
        if(size == 0){
            throw new IllegalArgumentException("BST is empty");
        }
       Node node =  minNode(root);
        return  node.e;
    }
    //一直向左找，找到最小的
    private Node minNode(Node node) {
        if(node.left ==null){
            return node;
        }
        return minNode(node.left);

    }


    //寻找BST的最大元素
    public E  maxNode(){
        if(size == 0){
            throw new IllegalArgumentException("BST is empty");
        }
        Node node =  maxNode(root);
        return  node.e;
    }
    //一直向右找，找到最小的
    private Node maxNode(Node node) {
        if(node.right ==null){
            return node;
        }
        return minNode(node.right);

    }

    //删除BST的最小值
    public E removeMin(){
        E ret = minNode();

        removeMin(root);
        return ret;
    }

    // 返回删除结点后的新的二分搜索树的根
    //分情况讨论
    private Node removeMin(Node node) {
        if(node.left ==null){
            Node rightNode = node.right;
            node.right=null;
            size--;
            return rightNode;
        }
        node.left =removeMin(node.left);
        return node;
    }


    //删除BST的最小值
    public E removeMax(){
        E ret = minNode();

        removeMax(root);
        return ret;
    }

    // 返回删除结点后的新的二分搜索树的根
    //分情况讨论
    private Node removeMax(Node node) {
        if(node.right ==null){
            Node leftNode = node.left;
            node.left=null;
            size--;
            return leftNode;
        }
        node.right =removeMax(node.right);
        return node;
    }



        public void  remove(E e){
        remove(root,e);
        }

    private Node remove(Node node, E e) {

        if(node == null){
            return null;
        }
        if(e.compareTo(node.e) < 0){
            node.left = remove(node.left,e);
            return node;
        }else if(e.compareTo(node.e) > 0){
             node.right = remove(node.right,e);
             return  node;
        }else{  //e ==node.e
            //待删除的结点左子树为null
            if(node.left == null){
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }

            //待删除的结点右子树为null
            if(node.right == null){
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }

            //待删除结点左右都不为null
            //找到比待删除结点大的最小结点--后继节点
            Node successor = minNode(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;

            node.left = node.right =null;
            return  successor;
        }


    }


    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        generateBSTString(root,0,res);
        return res.toString();
    }

    private void generateBSTString(Node node, int depth, StringBuilder res) {
        if(node==null){
            res.append(generateDepthString(depth)+"null\n");
            return;
        }

        res.append(generateDepthString(depth)+ node.e + "\n");
        generateBSTString(node.left,depth+1,res);
        generateBSTString(node.right,depth+1,res);

    }

    private String generateDepthString(int depth) {
       StringBuilder res= new StringBuilder();
        for (int i = 0; i < depth; i++) {
            res.append("--");
        }
        return  res.toString();
    }

}
